package com.shuwei.wificollecttool;

/**
 * Created by Halohoop on 2017/11/22.
 */

import android.os.Environment;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class WriteSheet {

    public static void write() throws Exception {

        //Create blank workbook
        HSSFWorkbook workbook = new HSSFWorkbook();

        //Create a blank sheet
        HSSFSheet spreadsheet = workbook.createSheet(
                " Employee Info ");

        //Create row object
        HSSFRow row;

        //This data needs to be written (Object[])
        Map<String, Object[]> empinfo =
                new TreeMap<String, Object[]>();
        empinfo.put("1", new Object[]{"EMP ID", "EMP NAME", "DESIGNATION"});
        empinfo.put("2", new Object[]{"tp01", "Gopal", "Technical Manager"});
        empinfo.put("3", new Object[]{"tp02", "Manisha", "Proof Reader"});
        empinfo.put("4", new Object[]{"tp03", "Masthan", "Technical Writer"});
        empinfo.put("5", new Object[]{"tp04", "Satish", "Technical Writer"});
        empinfo.put("6", new Object[]{"tp05", "Krishna", "Technical Writer"});

        //Iterate over data and write to sheet
        Set<String> keyid = empinfo.keySet();
        int rowid = 0;

        for (String key : keyid) {
            row = spreadsheet.createRow(rowid++);
            Object[] objectArr = empinfo.get(key);
            int cellid = 0;

            for (Object obj : objectArr) {
                Cell cell = row.createCell(cellid++);
                cell.setCellValue((String) obj);
            }
        }

        //Write the workbook in file system
        String s = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator
                + "Writesheet.xls";
        FileOutputStream out = new FileOutputStream(new File(s));
        workbook.write(out);
        out.close();
        System.out.println("Writesheet.xlsx written successfully");
    }

    public static void writeTitles(File file, Object[] titles) throws Exception {

        //Create blank workbook
        HSSFWorkbook workbook = new HSSFWorkbook();

        //Create a blank sheet
        HSSFSheet spreadsheet = workbook.createSheet(
                "Wifi采集");

        //Create row object
        HSSFRow row;

        //This data needs to be written (Object[])
        Map<String, Object[]> empinfo =
                new TreeMap<String, Object[]>();
        empinfo.put("1", titles);

        //Iterate over data and write to sheet
        Set<String> keyid = empinfo.keySet();
        int rowid = 0;

        for (String key : keyid) {
            row = spreadsheet.createRow(rowid++);
            Object[] objectArr = empinfo.get(key);
            int cellid = 0;

            for (Object obj : objectArr) {
                Cell cell = row.createCell(cellid++);
                cell.setCellValue((String) obj);
            }
        }

        //Write the workbook in file system
        FileOutputStream out = new FileOutputStream(file);
        workbook.write(out);
        out.close();
    }
    public static void writeRssis(File file, Map<String, Object[]> rssiMap, int startRowId) throws Exception {

        //Create blank workbook
        FileInputStream fileInputStream = new FileInputStream(file);
        HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);

        //get a old sheet
        HSSFSheet spreadsheet = workbook.getSheet("Wifi采集");

        //Create row object
        HSSFRow row;

        //Iterate over data and write to sheet
        Set<String> keyid = rssiMap.keySet();
        int rowid = startRowId;

        for (String key : keyid) {
            row = spreadsheet.createRow(rowid++);
            Object[] objectArr = rssiMap.get(key);
            int cellid = 0;

            for (Object obj : objectArr) {
                Cell cell = row.createCell(cellid++);
                cell.setCellValue("" + obj);
            }
        }
        fileInputStream.close();

        //Write the workbook in file system
        FileOutputStream out = new FileOutputStream(file);
        workbook.write(out);
        out.close();
    }
}
